import { Table } from 'antd';
import Cls from 'classnames';
import { AcquisitionManageContext, IAcquisitionActionEnum } from '../mconfig';
import { useCallback, useContext, useEffect, useMemo, useState } from 'react';
import { contractModalColumns } from '../mconfig';
import { useAcquisitionManageReducer } from '../mconfig';
import { getLoadMapList, getSelectDataMapList, getTenantID } from '@/utils';
import { ILoadMapTypeAll } from '@/common';

export const UpdateContractView: React.FC<any> = ({ formName, pageType }) => {
  // /**  获取数据字典 */
  const { loadMapInfo, mapLoading } = getLoadMapList([
    `${getTenantID()}${ILoadMapTypeAll.ASSET_TYPE}`, //资产类型
  ]);

  const { contractForm, materielList, contractMaterList, contractList, acquisitionDispatch } = useContext(AcquisitionManageContext);

  const delSelectRow = (record) => {
    // 取消选中的合同
    const arr = contractList.filter((item) => {
      return item.id !== record.id;
    });
    // 删除设置合同字段为空
    contractForm?.setFieldsValue({
      contractName: '',
    });

    // 处理数据  删除合同同时，删除合同带出来的物料
    const filterContractList: any = []; // 删除合同带出的物料
    const contractIds: any = []; // 合同带出的物料id集合
    contractMaterList.forEach((sub: any) => {
      contractIds.push(sub.id);
    });
    materielList.forEach((item: any) => {
      if (!contractIds.includes(item.id)) {
        filterContractList.push(item);
      }
    });

    // 更新物料列表
    acquisitionDispatch({
      type: IAcquisitionActionEnum.SELECT_MATERIEL,
      payload: filterContractList,
    });
    // 更新合同列表
    acquisitionDispatch({
      type: IAcquisitionActionEnum.SELECT_CONTRACT,
      payload: arr,
    });
  };

  /** 表格行配置 */
  const columns: Record<string, any>[] = useMemo(() => {
    return contractModalColumns(delSelectRow, pageType);
  }, [contractList]);

  useEffect(() => {}, [contractList]);

  return (
    <div className={Cls('list-table', 'descriptionsType-tableSty')} style={{ marginTop: '10px' }}>
      <Table bordered size="small" dataSource={contractList} columns={columns} rowKey={(res) => res?.id} scroll={{ x: 1200, y: '' }} pagination={false} />
    </div>
  );
};
